<?php
/**
 * 商城支付回调
 *
 *
 */

require_once "./api/payment/wpay/lib/WxPay.Api.php";
require_once './api/payment/wpay/lib/WxPay.Notify.php';
//require_once './api/payment/wpay/log.php';


class ReturnmallpaymentAction extends Action{

	public function __construct() {
		parent::__construct();
	}
   /**
 * 通用通知接口demo
 * ====================================================
 * 支付完成后，微信会把相关支付和用户信息发送到商户设定的通知URL，
 * 商户接收回调信息后，根据需要设定相应的处理流程。
 * 
 * 这里使用log文件形式记录回调信息。 以及更新订单状态等
*/
public function index() {
    //初始化日志

    $logHandler= new CLogFileHandler("./api/payment/wpay/logs/".date('Y-m-d').'.log');
    $log = Logs::Init($logHandler, 15);
     
    Logs::DEBUG("\n begin notify :");
    $notify = new PayNotifyCallBack();
    $notify->Handle(false);
	Logs::DEBUG("\n end notify");
}
 /**
 * 通用通知接口demo
 * ====================================================
 * 支付完成后，微信会把相关支付和用户信息发送到商户设定的通知URL，
 * 商户接收回调信息后，根据需要设定相应的处理流程。
 * 
 * 这里使用log文件形式记录回调信息。 以及更新订单状态等
*/
public function test() {
    //初始化日志

    $logHandler= new CLogFileHandler("./api/payment/wpay/logs/".date('Y-m-d').'.log');
    $log = Logs::Init($logHandler, 15);
     
    Logs::DEBUG("\n begin test");
    $notify = new PayNotifyCallBack();
    $notify->send_test();
}
//定时发送
/*
$time = '2010-05-27 12:11';
$res = sendSMS($uid,$pwd,$mobile,$content,$time);
echo $res;
*/
function sendSMS($order_sn=null,$money=null)
{
 if(empty($order_sn) || empty($money)){
    return false;
 }
 $mobile=C('SENDMOBILE');
 if(empty($mobile)){
    return false;
 }
 Logs::DEBUG("\n短信发送中:$mobile  .. $order_sn   ... $money");
  $http = 'http://http.yunsms.cn/tx/';//接口地址 'http://接口地址/tx/';
  $content="FG峰购产生一条新订单，订单号：".$order_sn."，金额,￥".$money."，时间".date("Y-m-d H:i")."微信支付";
  $data = array
    (
    'uid'=>C('SENDUID'),          //用户账号
    'pwd'=>strtolower(md5(C('SENDPWD'))), //MD5位32密码
    'mobile'=>$mobile,        //号码
    'content'=>$content,      //内容 如果对方是utf-8编码，则需转码iconv('gbk','utf-8',$content); 如果是gbk则无需转码
    'time'=>$time,    //定时发送
    'mid'=>$mid,           //子扩展号
    'encode'=>'utf8'
    );
  $re= $this->postSMS($http,$data);      //POST方式提交
  $return_data['status']=0;
  if( trim($re) == '100' )
  {
     $datas=json_encode($data);
    //$return_data['status']=1;
    //return $return_data;
     Logs::DEBUG("\n短信发送成功:".$datas);
  }
  else 
  {
     $datas=json_encode($data);
    //return "发送失败! 状态：".$re;
    $return_data['error']="";
    Logs::DEBUG("\n短信发送失败:".$datas);
  }
}    
public function postSMS($url,$data='')
{
  $post='';
  $row = parse_url($url);
  $host = $row['host'];
  $port = $row['port'] ? $row['port']:80;
  $file = $row['path'];
  while (list($k,$v) = each($data)) 
  {
    $post .= rawurlencode($k)."=".rawurlencode($v)."&"; //转URL标准码
  }
  $post = substr( $post , 0 , -1 );
  $len = strlen($post);
  $fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
  if (!$fp) {
    return "$errstr ($errno)\n";
  } else {
    $receive = '';
    $out = "POST $file HTTP/1.0\r\n";
    $out .= "Host: $host\r\n";
    $out .= "Content-type: application/x-www-form-urlencoded\r\n";
    $out .= "Connection: Close\r\n";
    $out .= "Content-Length: $len\r\n\r\n";
    $out .= $post;    
    fwrite($fp, $out);
    while (!feof($fp)) {
      $receive .= fgets($fp, 128);
    }
    fclose($fp);
    $receive = explode("\r\n\r\n",$receive);
    unset($receive[0]);
    return implode("",$receive);
  }
}

  
}


class PayNotifyCallBack extends WxPayNotify
{
	 //查询订单
    public function send_test()
    {    
        $rep=new ReturnmallpaymentAction();
        $rep->sendSMS('22222',11);
    }
    //查询订单
    public function Queryorder($transaction_id)
    {    
        $input = new WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = WxPayApi::orderQuery($input);
        Logs::DEBUG("query:" . json_encode($result));
        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS")
        {
            return true;
        }
        return false;
    }
    
    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        Logs::DEBUG("\n call back:" . json_encode($data));
        $notfiyOutput = array();
        
        if(!array_key_exists("transaction_id", $data)){
			
            $msg = "输入参数不正确";
			 Logs::DEBUG("\n ERROR:" . $msg);
            return false;
        }
		// Logs::DEBUG("------noerror1---------");
        //查询订单，判断订单真实性
        if(!$this->Queryorder($data["transaction_id"])){
            $msg = "订单查询失败";
			 Logs::DEBUG("\n ERROR:" . $msg);
            return false;
        }
		// Logs::DEBUG("------noerror---------");
         //商户订单号
            $out_trade_no = $data['out_trade_no'];
            //交易号
            $trade_no = $data['transaction_id'];
            //支付接口代码
            $payment_code = 'wxpay';
            //验证成功      
            $model_order = D('Mallorder');
            $model_payment = D('Mallpayment');
        //pay_status 支付状态；0，未付款；1，付款中 ；2，已付款
            $order_list = $model_order->getOrderList(array('order_sn'=>$out_trade_no,'pay_status'=>'0'));

            $result = $model_payment->updateProductBuy($out_trade_no, $payment_code, $order_list, $trade_no);
            if(empty($result['error'])) {
                $money=$data["cash_fee"]/100;


                //购买成功  查询是否是会员 不是会员则升级为会员
                $log=array();
                $member=M('member')->where(array('id'=>$order_list[0]['user_id']))->field('id, member_card, member_vip_type')->find();
                if($order_list[0]['is_upgrade']){//升级订单
                    if($member && $member['member_vip_type']<2) {
                       // M('member')->where(array('id' => $order_list[0]['user_id']))->save(array('member_vip_type' => 1,'vip_time'=>time()));
                        $upgrade_ob = A("Start");
						$ress = $upgrade_ob->upgrade_level($out_trade_no);
						Logs::DEBUG("\n 会员" . $member['member_card'] . "升级VIP成功");
                        $log['des'] = '用户使用微信支付￥' . $money . '升级会员成功';
                    }else{
                        $log['des'] = '用户使用微信支付￥'.$money;
                    }
                }else{
                    $log['des'] = '用户使用微信支付￥'.$money;
                }
                //记录订单日志
                $data = array();
                $data['order_id'] = $order_list[0]['order_id'];
                $data['log_role'] = 'buyer';
                $data['log_msg'] = $log['des'];
                $data['log_orderstate'] = '20';
                $insert = $model_order->addOrderLog($data);
               
                Logs::DEBUG("\n update success:订单更新成功");
                $rep=new ReturnmallpaymentAction();
                $rep->sendSMS($out_trade_no,$money);
                return true;
            }else{
				 Logs::DEBUG("\n update error:" . json_encode($result));
               return false;
            }
        
        return true;
    }
}

//以下为日志

interface ILogHandler
{
    public function write($msg);
    
}

class CLogFileHandler implements ILogHandler
{
    private $handle = null;
    
    public function __construct($file = '')
    {
        $this->handle = fopen($file,'a');
    }
    
    public function write($msg)
    {
        fwrite($this->handle, $msg, 4096);
    }
    
    public function __destruct()
    {
        fclose($this->handle);
    }
}

class Logs
{
    private $handler = null;
    private $level = 15;
    
    private static $instance = null;
    
    private function __construct(){}

    private function __clone(){}
    
    public static function Init($handler = null,$level = 15)
    {
        if(!self::$instance instanceof self)
        {
            self::$instance = new self();
            self::$instance->__setHandle($handler);
            self::$instance->__setLevel($level);
        }
        return self::$instance;
    }
    
    
    private function __setHandle($handler){
        $this->handler = $handler;
    }
    
    private function __setLevel($level)
    {
        $this->level = $level;
    }
    
    public static function DEBUG($msg)
    {
        self::$instance->write(1, $msg);
    }
    
    public static function WARN($msg)
    {
        self::$instance->write(4, $msg);
    }
    
    public static function ERROR($msg)
    {
        $debugInfo = debug_backtrace();
        $stack = "[";
        foreach($debugInfo as $key => $val){
            if(array_key_exists("file", $val)){
                $stack .= ",file:" . $val["file"];
            }
            if(array_key_exists("line", $val)){
                $stack .= ",line:" . $val["line"];
            }
            if(array_key_exists("function", $val)){
                $stack .= ",function:" . $val["function"];
            }
        }
        $stack .= "]";
        self::$instance->write(8, $stack . $msg);
    }
    
    public static function INFO($msg)
    {
        self::$instance->write(2, $msg);
    }
    
    private function getLevelStr($level)
    {
        switch ($level)
        {
        case 1:
            return 'debug';
        break;
        case 2:
            return 'info';  
        break;
        case 4:
            return 'warn';
        break;
        case 8:
            return 'error';
        break;
        default:
                
        }
    }
    
    protected function write($level,$msg)
    {
        if(($level & $this->level) == $level )
        {
            $msg = "\r\n ".'['.date('Y-m-d H:i:s').']['.$this->getLevelStr($level).'] '.$msg."\n";
            $this->handler->write($msg);
        }
    }



   } 